/**
 * 用户user.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    $("#user_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/user/userList.do',
        idField: 'id',
        striped:true,
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'loginName', title: '登录名称', width: 150, fixed: true},
                {field: 'userPhone', title: '用户手机', width: 150, fixed: true},
                {field: 'company', title: '所属公司', width: 210, fixed: true, formatter: function (val) {
                        if(val) {
                            return val.name;
                        }
                    }},
                {field: 'employee', title: '所属员工', width: 110, fixed: true, formatter: function (val) {
                        if(val) {
                            return val.name;
                        }
                    }},
                {field: 'loginStatus', title: '登录状态', width: 120, fixed: true, formatter: function (val) {
                        if(val){
                            if(val == "1"){
                                return "<span style='color: green; font-weight: bold;'>可登录</span>";
                            }else{
                                return "<span style='color: red; font-weight: bold;'>已禁用</span>";
                            }
                        }
                    }},
                {field: 'receiveAlarm', title: '接收警报', width: 80, fixed: true, formatter: function (val) {
                        if(val){
                            if(val == "1"){
                                return "<span style='color: green; font-weight: bold;'>接收</span>";
                            }else{
                                return "<span style='color: red; font-weight: bold;'>不接收</span>";
                            }
                        }
                    }},
                {field: 'createDate', title: '创建时间', width: 160, fit: true, formatter: function (val) {
                        if(val) {
                            return formatDatebox(val, "yyyy-MM-dd HH:mm:ss", true);
                        }
                    }}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#user_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#user_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#user_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#user_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

    //查询所属公司下拉树
    $.ajax({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        type: "get",
        success: function (data) {
            var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
            themecombo += data.substring(1);
            themecombo = JSON.parse(themecombo);
            $("#companyTree").combotree("loadData",themecombo);
            $("#companyTree").combotree({onSelect:function(node){
                        $('#companyId_').val(node.id);
                    }});
        }
    });
});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/user/toUserOption.do";
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            user_edit();
                        }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            user_del();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            user_findList();
                        }
                    });
                }
                //刷新按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            user_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除用户
 */
function user_del() {
    var row = $("#user_dataList").datagrid("getSelected");
    var checked = $("#user_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择删除用户", timeout: 3000, showType: "slide" });
        return;
    }
    //验证删除账号是否是主账号
    if(row.id == "1"){
        $.messager.show({ title: '操作提示', msg: "超级管理员用户不能被删除！", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.loginName + ' 用户吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/user/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#user_dataList").datagrid("reload", {
                        loginName:$("#loginName_").val(),
                        userPhone:$("#userPhone_").val(),
                        companyId:$("#companyId_").val(),
                        loginStatus:$("#loginStatus_").combobox("getValue"),
                        receiveAlarm:$("#receiveAlarm_").combobox("getValue")
                    });
                }
            })
        }
    });
}

/**
 * 修改用户
 * @returns {boolean}
 */
function user_edit(){
    var row = $("#user_dataList").datagrid("getSelected");
    var checked = $("#user_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title:'操作提示', msg:"请选择修改用户", timeout:3000, showType:"slide" });
        return;
    }
    window.location.href = CONTEXT_PATH + "/user/toUserOption.do?id=" + row.id;
}

/**
 * 用户详情
 */
function user_view(iconCls){
    var row = $("#user_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的用户详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(yhxq.id, yhxq.url + row.id, iconCls, yhxq.closable, yhxq.title);
}

//查询用户
function user_findList(){
    $("#user_dataList").datagrid("reload",{
        loginName:$("#loginName_").val(),
        userPhone:$("#userPhone_").val(),
        companyId:$("#companyId_").val(),
        loginStatus:$("#loginStatus_").combobox("getValue"),
        receiveAlarm:$("#receiveAlarm_").combobox("getValue")
    });
    //清楚选中项
    $("#user_dataList").datagrid("clearSelections");
}
